package pku;

import java.io.IOException;

/**
 * Created by yangxiao on 2017/11/14.
 */
public class Producer {
    private DefaultMessageFactory messageFactory = new DefaultMessageFactory();
    private MessageWriter messageWriter;
    private KeyValue properties;

    public Producer(KeyValue properties){
        this.properties = properties;
        messageWriter = new MessageWriter(properties);
        new Thread(messageWriter).start();
    }

    public Producer() {
        messageWriter = new MessageWriter();
        new Thread(messageWriter).start();
    }

    public KeyValue properties() { return properties; }

    public ByteMessage createBytesMessageToTopic(String topic, byte[] body)throws Exception{
        return messageFactory.createBytesMessageToTopic(topic,body);
    }
    public void send(ByteMessage defaultMessage)throws Exception{
        messageWriter.addMessage(defaultMessage      );
    }

    public void flush()throws IOException {
        ByteMessage FIN = messageFactory.createBytesMessageToQueue("", "".getBytes());
        try {
            send(FIN);
            System.out.println(1);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
